package gui;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Set;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import containers.StudentContainer;
import entities.CoSigner;

/**
 * Panel for entry of loan information and password displays user id
 * 
 * @author gke703
 */
public class LoanInfoPanel extends JPanel {

	/** button for applying for loan */
	protected JButton confirmButton;

	/** field for requesting a loan amount */
	protected ValueEntryPanel amountField;

	/** field for entering a password */
	protected ValueEntryPanel passwordField;

	/** button to get to cosigner application */
	protected JButton cosignerButton;

	/** cosigner that was created in cosigner application */
	protected CoSigner appCosigner;

	/**
	 * displays loan info, forms info entry
	 */
	public LoanInfoPanel() {

		setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));

		// add(Box.createHorizontalGlue());

		// add a label with a prompt to enter the new financial info
		JLabel prompt = new JLabel("Confirm Loan Application");
		prompt.setMaximumSize(prompt.getPreferredSize());
		add(prompt);
		prompt.setAlignmentX(Component.LEFT_ALIGNMENT);
		add(Box.createVerticalGlue());

		// add a txtbox with the field for the entry of the amount requested for
		// the loan
		amountField = new ValueEntryPanel("Amount Requested:");
		add(amountField);
		add(amountField);
		amountField.setAlignmentX(Component.LEFT_ALIGNMENT);
		add(Box.createVerticalGlue());

		Set<Integer> ids = StudentContainer.dictionary().keySet();
		Object[] idsArray = ids.toArray();

		// find out next best integer for new user id
		int newID = 123455;

		for (int i = 0; i < idsArray.length; i++) {
			if (newID < (Integer) idsArray[i]) {
				newID = (Integer) idsArray[i];
			}
		}

		newID = newID + 1;

		// add a label with a prompt stating what the user's login id will be
		JLabel idPrompt = new JLabel("User ID:   " + newID);
		idPrompt.setMaximumSize(idPrompt.getPreferredSize());
		add(idPrompt);
		idPrompt.setAlignmentX(Component.LEFT_ALIGNMENT);
		add(Box.createVerticalGlue());

		// add a txtbox with the field for the entry of the student's login
		// password
		passwordField = new ValueEntryPanel("Password:");
		add(passwordField);
		add(passwordField);
		passwordField.setAlignmentX(Component.LEFT_ALIGNMENT);
		add(Box.createVerticalGlue());

		// add a button to initiate the application
		cosignerButton = new JButton("Cosigner Application");
		cosignerButton.setMaximumSize(cosignerButton.getPreferredSize());
		add(cosignerButton);
		cosignerButton.setAlignmentX(Component.LEFT_ALIGNMENT);
		add(Box.createVerticalGlue());

		final LoanInfoPanel thisPanel = this;
		cosignerButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				CosignerFrame coFrame = new CosignerFrame(thisPanel);
				coFrame.setVisible(true);
			}
		});

		// add a button to initiate the application
		confirmButton = new JButton("Apply");
		confirmButton.setMaximumSize(confirmButton.getPreferredSize());
		add(confirmButton);
		confirmButton.setAlignmentX(Component.LEFT_ALIGNMENT);
		add(Box.createVerticalGlue());
		// ApplyListener listener = new ApplyListener();
		// confirmButton.addActionListener(listener);

	}

	private static final long serialVersionUID = 1L;

}
